package br.com.criativasystem.controle;

import br.com.criativasystem.modelo.Login;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Classe
 * de
 * persistencia
 * do
 * login
 *
 * @author
 * Lucas
 * Calixto
 */
public class DAOLogin {

    private Socket socket;
    private ObjectOutputStream saida;
    private ObjectInputStream entrada;
    private Login login;
    private Boolean logar;

    public DAOLogin() {
        login = new Login();
    }

    public Boolean selecionaUsuario(Login l) {
        try {
            socket = new Socket("localhost", 5300);
            saida = new ObjectOutputStream(socket.getOutputStream());
            login.setUsuario(l.getUsuario());
            login.setSenha(l.getSenha());
            saida.writeObject(login);

            entrada = new ObjectInputStream(socket.getInputStream());
            logar = (Boolean) entrada.readObject();

        } catch (UnknownHostException ex) {
            System.out.println("Host não encontrado");
        } catch (IOException ex) {
            System.out.println("Erro ao escrever ou ler o arquivo");
        } catch (ClassNotFoundException ex) {
            System.out.println("Classess não encontradas");
        } finally {
            try {
                saida.close();
                entrada.close();
                socket.close();
            } catch (IOException e) {
                System.out.println("Erro ao fechar conexões");
            }
        }
        return logar;
    }
}
